package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.BreadCrumbItem;
import util.Script;

/**
 * Trabalho PD - 2014/2015
 *
 * @author a21210380 a21210392
 */
public abstract class ServletBase extends HttpServlet {
    public final static String JSP_NOT_AUTHORIZED = "/WEB-INF/jsp/notauthorized.jsp";
    public static final String JSP_NOT_FOUND_PATH = "/WEB-INF/jsp/notfound.jsp";
    public static final String JSP_INVALID_ARGUMENTS = "/WEB-INF/jsp/invalidrequest.jsp";
    public static final String JSP_NAVBAR = "/WEB-INF/jsp/navbar.jsp";

    public final static String JSP_HOMEPAGE = "/WEB-INF/jsp/homepage.jsp";
    public final static String JSP_LOGIN = "/WEB-INF/jsp/visitor/login.jsp";
    public final static String JSP_REGISTER_USER = "/WEB-INF/jsp/visitor/registeruser.jsp";
    public final static String JSP_SEARCH_TRIP = "/WEB-INF/jsp/searchtrip.jsp";
    
    public final static String JSP_USER_ACCOUNT_CONFIGURATION = "/WEB-INF/jsp/user/useraccountconfiguration.jsp";
    public final static String JSP_CANCEL_ACCOUNT = "/WEB-INF/jsp/user/cancelAccount.jsp";
    public final static String JSP_MY_TRIPS = "/WEB-INF/jsp/user/mytrips.jsp";
    public final static String JSP_CREATE_TRIP = "/WEB-INF/jsp/user/createTrip.jsp";
    public final static String JSP_FORUM = "/WEB-INF/jsp/user/forum.jsp";
    public final static String JSP_EMAIL = "/WEB-INF/jsp/user/emails.jsp";
    public final static String JSP_SEE_TOPIC = "/WEB-INF/jsp/user/seetopic.jsp";
    public final static String JSP_TRIP_INFORMATION = "/WEB-INF/jsp/user/tripinformation.jsp";
    public final static String JSP_SEE_CARS = "/WEB-INF/jsp/user/seeCars.jsp";
    public final static String JSP_EVALUATE = "/WEB-INF/jsp/user/evaluate.jsp";

    public final static String JSP_ADMIN_ACCOUNT_CONFIGURATIONS = "/WEB-INF/jsp/admin/accountconfigurations.jsp";
    public final static String JSP_ADD_LOCATION = "/WEB-INF/jsp/admin/addlocation.jsp";
    public final static String JSP_ACCOUNTS = "/WEB-INF/jsp/admin/accounts.jsp";
    public final static String JSP_SERVER_CONFIGURATIONS = "/WEB-INF/jsp/admin/serverconfigurations.jsp";

    public final static String JSP_TESTE = "/WEB-INF/jsp/testarDataTAble.jsp";

    protected final String title;
    protected BreadCrumbItem[] breadCrumbItens;
    protected String[] extraStyles;
    protected Script[] extraScripts;

    public ServletBase(String title, BreadCrumbItem[] breadCrumbItens, String[] extraStyles, Script[] extraScripts) {
        this.title = title;
        this.breadCrumbItens = breadCrumbItens;
        this.extraStyles = extraStyles;
        this.extraScripts = extraScripts;
    }
    
    @Override
    protected final void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("pageTitle", title);
        
        if(breadCrumbItens != null)
            req.setAttribute("breadcrumb", breadCrumbItens);
        
        if(extraStyles != null)
            req.setAttribute("extraStyles", extraStyles);
        
        if(extraScripts != null)
            req.setAttribute("extraScripts", extraScripts);
        
        onGet(req, resp);
    }

    @Override
    protected final void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("pageTitle", title);
        
        if(breadCrumbItens != null)
            req.setAttribute("breadCrumbItens", breadCrumbItens);
        
        if(extraStyles != null)
            req.setAttribute("extraStyles", extraStyles);
        
        if(extraScripts != null)
            req.setAttribute("extraScripts", extraScripts);
        
        onPost(req, resp);
    }
    
    protected abstract void onGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
    protected abstract void onPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
    

    protected void callJsp(String view, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        getServletConfig().getServletContext().getRequestDispatcher(view).forward(req, resp);
    }

    protected void redirect(String url, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Refresh", "0; URL=" + url);
        try (PrintWriter pw = resp.getWriter()) {
            pw.println(" ");
        }
    }

    protected void writeText(String text, HttpServletResponse resp) throws ServletException, IOException {
        try (PrintWriter pw = resp.getWriter()) {
            pw.println(text);
        }
    }

}
